package com.util.zk;

import com.bean.zk.Power;

import java.util.*;

public class TreeJsonUtil {


    public static List<Power> getTreeJson(List<Power> powers ,List<Power> checked) {

        //父节点
         List<Power> fatherNodeList = new ArrayList<Power>();
        //子节点
         List<Power> childrenNodeList = new ArrayList<Power>();
        //处理后的数据
         List<Power> treelist = new ArrayList<Power>();

         //先将所有的title赋值
        for (Power power : powers) {
            power.setTitle(power.getPower_name());
            power.setId(power.getPower_id());
            for (Power power1 : checked) {
                if (Objects.equals(power1.getPower_id(), power.getPower_id())) {
                    power.setChecked(true);
                }
            }

        }

        //       获取所有子节点和父节点
        for (Power power : powers) {
            //获取节点id
            Integer id = power.getPower_rootid();
            if (id==0) {
                fatherNodeList.add(power);
            } else {
                childrenNodeList.add(power);
            }
        }

        for (Power power : fatherNodeList) {
            //父节点id
            Integer id = power.getPower_id();
            List<Power> child = new ArrayList<Power>();
            for (Power childpower : childrenNodeList) {
                Integer childid = childpower.getPower_rootid();
                if (childid==id) {
                    int a = 0;
                    child.add(childpower);
                    a++;
                }
            }

            power.setChildren(child);
            treelist.add(power);
        }
        return treelist;
    }


}
